package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportNews;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportNewsDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-05-18 20:14:21
 */
public class TransportNewsConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportNewsConverts.class);

    public static List<TransportNewsDTO> convert(List<TransportNews> transportNewsList) {
        if (CollectionUtils.isEmpty(transportNewsList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportNewsDTO> transportNewsListResult = Lists.newArrayList();
        for (TransportNews transportNews : transportNewsList) {
            transportNewsListResult.add(convert(transportNews));
        }
        return transportNewsListResult;
    }

    public static TransportNewsDTO convert(TransportNews transportNews) {
        try {
            return BeanUtils.copy(transportNews, TransportNewsDTO.class);
        } catch (Exception e) {
            LOG.error("TransportNews到TransportNewsDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportNews到TransportNewsDTO转换失败", e);
        }
    }

    public static TransportNews convert(TransportNewsDTO transportNewsDTO) {
        try {
            return BeanUtils.copy(transportNewsDTO, TransportNews.class);
        } catch (Exception e) {
            LOG.error("TransportNewsDTO到TransportNews转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportNewsDTO到TransportNews转换失败", e);
        }
    }
}